package com.kandinfo.isbnmanage.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kandinfo.base.admin.service.DictDataService;
import com.kandinfo.base.security.domain.User;
import com.kandinfo.base.security.service.UserService;
import com.kandinfo.isbnmanage.domian.IsbnDistribute;
import com.kandinfo.isbnmanage.domian.IsbnInformation;
import com.kandinfo.isbnmanage.domian.IsbnOrder;
import com.kandinfo.isbnmanage.dto.IsbnCirculationDto;
import com.kandinfo.isbnmanage.dto.IsbnCirculationReqDto;
import com.kandinfo.isbnmanage.dto.IsbnDistributeDto;
import com.kandinfo.isbnmanage.repository.IsbnDistributeRepository;
import com.kandinfo.isbnmanage.service.IsbnCirculationService;
import com.kandinfo.isbnmanage.service.IsbnInformationService;
import com.kandinfo.isbnmanage.service.IsbnOrderService;

@Service
public class IsbnCirculationServiceImpl implements IsbnCirculationService {
	
	@Autowired
	IsbnInformationService isbnInformationService;
	@Autowired
	IsbnOrderService isbnOrderService;
	@Autowired
	IsbnDistributeRepository isbnDistributeRepository;
	@Autowired
	UserService userService;
	@Autowired
	DictDataService dictDataService;
	
	@Override
	public List<IsbnCirculationDto> getCircuInfo(IsbnCirculationReqDto reqDto) {
		
		Map<Long, User> userMap = userService.getUserMap();
		
		String barcode = reqDto.getBarcode();
		
		List<IsbnInformation> informationList = isbnInformationService.findByBarcode(barcode);

		List<IsbnCirculationDto> isbnCirculationDtos = new ArrayList<IsbnCirculationDto>();
		IsbnCirculationDto circulationDto = null;
		
		if (CollectionUtils.isEmpty(informationList)) {
			circulationDto = new IsbnCirculationDto();
			
			IsbnDistribute isbnDistribute = isbnDistributeRepository.findByBarcodeAndType(barcode, 0);
		
			List<IsbnDistributeDto> isbnDistributeDtoList = new ArrayList<IsbnDistributeDto>();
			
			IsbnDistributeDto dto = new IsbnDistributeDto();
			BeanUtils.copyProperties(isbnDistribute, dto);
			
			Long userId = isbnDistribute.getDistributeTo();
			if (userId != null) {
				dto.setDistributeToName(userMap.get(userId).getName());
			}else{
				dto.setDistributeToName(userId +"");
			}
			
			isbnDistributeDtoList.add(dto);
			
			setCirculationDto(circulationDto, null, null, isbnDistributeDtoList ,userMap);

			isbnCirculationDtos.add(circulationDto);
		}else {
			for (IsbnInformation isbnInformation : informationList) {

				circulationDto = new IsbnCirculationDto();
				Long isbnInformationId = isbnInformation.getId();
				
				IsbnOrder isbnOrder = isbnOrderService.get(barcode, isbnInformationId);
				
				List<IsbnDistribute> isbnDistributeList = isbnDistributeRepository.findByInfoId(isbnInformationId);
				IsbnDistribute first = isbnDistributeRepository.findByBarcodeAndType(barcode, 0);
				isbnDistributeList.add(0, first);
			
				List<IsbnDistributeDto> isbnDistributeDtoList = new ArrayList<IsbnDistributeDto>();
				
				for (IsbnDistribute isbnDistribute : isbnDistributeList) {
					IsbnDistributeDto dto = new IsbnDistributeDto();
					BeanUtils.copyProperties(isbnDistribute, dto);
					
					Long userId = isbnDistribute.getDistributeTo();
					if (userId != null) {
						dto.setDistributeToName(userMap.get(userId).getName());
					}else{
						dto.setDistributeToName(userId +"");
					}
					
					isbnDistributeDtoList.add(dto);
				}
				
				setCirculationDto(circulationDto, isbnInformation, isbnOrder, isbnDistributeDtoList ,userMap);

				isbnCirculationDtos.add(circulationDto);

			}
		}

		return isbnCirculationDtos;
	}
	
	
	private void setCirculationDto(IsbnCirculationDto circulationDto, IsbnInformation isbnInformation,
			IsbnOrder isbnOrder, List<IsbnDistributeDto> isbnDistributeDtoList ,Map<Long, User> userMap) {
		// information
		if (isbnInformation != null) {
			if (StringUtils.isNotBlank(isbnInformation.getBarcode())) {
				circulationDto.setBarcode(isbnInformation.getBarcode());
			}
			if (StringUtils.isNotBlank(isbnInformation.getName())) {
				circulationDto.setInformationName(isbnInformation.getName());
			}
			if (StringUtils.isNotBlank(isbnInformation.getTime())) {
				circulationDto.setInformationTime(isbnInformation.getTime());
			}
			if (StringUtils.isNotBlank(isbnInformation.getPress())) {
				circulationDto.setInformationPress(isbnInformation.getPress());
			}
		}

		// order
		if (isbnOrder != null) {
			if (isbnOrder.getCreateBy() != null) {
				User createUser = userMap.get(isbnOrder.getCreateBy());
				if (createUser != null) {
					circulationDto.setOrderCreateByName(createUser.getName());
				} else {
					circulationDto.setOrderCreateByName("用户不存在或已被删除");
				}
				
			}
			if (isbnOrder.getCreateTs() != null) {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				String createTsStr = simpleDateFormat.format(isbnOrder.getCreateTs());
				
				circulationDto.setOrderCreateTsStr(createTsStr);
			}
			if (StringUtils.isNotBlank(isbnOrder.getOrderNum())) {
				circulationDto.setOrderNum(isbnOrder.getOrderNum());
			}
			if (StringUtils.isNotBlank(isbnOrder.getPurchasePlatform())) {
				circulationDto.setOrderPurchasePlatform(isbnOrder.getPurchasePlatform());
			}
		}

		//Distribute
		circulationDto.setIsbnDistributeDtoList(isbnDistributeDtoList);
	}
	
}
